package example.exception;

/**
 * Created by lenovo on 2016/11/7 0007.
 * try...catch模块，搭配finally
 */
public class Finally {
    public static void main(String[] args) {
        new Finally().doTheWork();
    }

    private void doTheWork() {
        Object o = null;
        for (int i = 0; i < 5; i++) {
            try {
                o = makeObj(i);
            } catch (IllegalArgumentException e) {
                System.err.println
                        ("Error: (" + e.getMessage() + ").");
                return;
            } finally {
                System.err.println("都已执行完毕");
                if (o == null)
                    System.exit(0);
            }
            System.out.println(o);
        }
    }

    private Object makeObj(int type)
            throws IllegalArgumentException {
        if (type == 1)
            throw new IllegalArgumentException
                    ("不是指定的类型： " + type);
        return new Object();
    }
}
